/**
 * @Company: 小明
 * @Author: 40313
 * @DATE: 2020/12/19 星期六 下午 2:07
 */
package com.situ.oa.index.service.impl;

import com.situ.oa.index.service.IndexService;
import com.situ.oa.user.dao.UserDao;
import com.situ.oa.user.pojo.User;
import com.situ.oa.util.ConfigUtil;
import com.situ.oa.util.MD5Util;
import com.situ.oa.util.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * ClassName: IndexServiceImpl
 *
 * @Author: 40313
 * @Description:(类描述)
 */
@Repository("indexService")
public class IndexServiceImpl implements IndexService {
    private UserDao userDao;

    @Autowired
    public IndexServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * #执行用户登录(如果登录界面的自动登录已勾选，则往浏览器中写入cookie值)
     *
     * @param userCode  用户账号
     * @param userPass  用户密码
     * @param autologin 自动登录
     * @param request   请求 根据request得到session对象
     * @param response  响应 将书写的cookie值响应给浏览器
     * @return
     */

    @Override
    public int doUserLogin(String userCode, String userPass, String autologin, HttpServletRequest request, HttpServletResponse response) {
        /**对得到的密码进行加密*/
        String encodeUserPass = MD5Util.encode(userPass);
        /**初始化返回值：1：正常登录，2：用户名或密码错误，3：用户被锁定*/
        int result = 0;
        User user = userDao.selectOneByuserCodeAnduserPass(userCode, encodeUserPass);
        HttpSession session = request.getSession();
        if (user != null) {
            /**用户没有被锁定*/
            if (user.getYesnoLock() == User.IS_LOCK_NO) {
                /**更新登录信息*/
                user.setLoginCount(user.getLoginCount() + 1);
                user.setLastLoginIp(ServletUtil.getIpAddr(request));
                user.setLastLoginDate(new Date());
                userDao.updateLoginInfo(user);
                /**往session中塞入登录的用户对象*/
                session.setAttribute(ConfigUtil.SESSION_LOGIN_USER_NAME, user);
                result = 1;
                if ("1".equals(autologin)) {
                    /**
                     * 拼写一个cookis，并写入到浏览器中
                     */
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(user.getLoginAccount()).append("~#~").append(user.getRowId());
                    Cookie cookie = new Cookie(ConfigUtil.COOKIE_NAME, stringBuilder.toString());
                    cookie.setMaxAge(60 * 60 * 24);
                    response.addCookie(cookie);
                }
            } else {
                result = 3;
            }

        } else {
            result = 2;
        }
        return result;
    }

    /**
     * #用于检查是否自动登录，如果浏览器中有cookie值，则根据cookie的value信息查询到用户的实例，并往session中塞入用户实例
     *
     * @param request 根据request得到cookie数组
     */
    @Override
    public void checkAutoLogin(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (ConfigUtil.COOKIE_NAME.equals(cookie.getName())) {
                    String value = cookie.getValue();
                    if (value != null) {
                        String[] array = value.split("~#~");
                        String loginAccount = array[0];
                        Long rowId = Long.parseLong(array[1]);
                        User user = userDao.selectOneByCodeAndId(loginAccount, rowId);
                        if (user != null) {
                            HttpSession session = request.getSession();
                            session.setAttribute(ConfigUtil.SESSION_LOGIN_USER_NAME, user);
                        }
                    }
                    break;
                }
            }
        }
    }
}
